package vn.info.vietnaminfo.db;

import java.util.ArrayList;
import java.util.List;

import vn.info.vietnaminfo.data.DSDiaDiem;
import vn.info.vietnaminfo.utli.CalendarUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class DaoDiaDiem extends BaseDao
{
	public static final String TABLE_NAME 			= "DIA_DIEM";
	private static final String ID 					= "ID";
	private static final String MA_DIA_DIEM 		= "MA_DIA_DIEM";
	private static final String TEN_DIA_DIEM 		= "TEN_DIA_DIEM";
	private static final String TEN_LOAI_HINH_KD 	= "TEN_LOAI_HINH_KD";
	private static final String LOAI_DIA_DIEM 		= "LOAI_DIA_DIEM";
	private static final String DIA_CHI 			= "DIA_CHI";
	private static final String QUAN 				= "QUAN";
	private static final String HUYEN 				= "HUYEN";
	private static final String PHUONG 				= "PHUONG";
	private static final String THANH_PHO 			= "THANH_PHO";
	private static final String PHONE_NUMBER 		= "PHONE_NUMBER";
	private static final String FAX 				= "FAX";
	private static final String MO_TA_LOAI_HINH_KD 	= "MO_TA_LOAI_HINH_KD";
	private static final String WEBSITE 			= "WEBSITE";
	private static final String URL_IMAGE 			= "URL_IMAGE";
	private static final String LAT 				= "LAT";
	private static final String LONG 				= "LONG";
	private static final String REG_UPD_DT			= "REG_UPD_DT";
    public static final String CREATE_TABLE 		= "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + MA_DIA_DIEM + " TEXT, " + TEN_DIA_DIEM + " TEXT, " + TEN_LOAI_HINH_KD + " TEXT, " + LOAI_DIA_DIEM + " TEXT, " + DIA_CHI + " TEXT, " + QUAN + " TEXT, " + HUYEN + " TEXT, " + PHUONG + " TEXT, " + THANH_PHO + " TEXT, " + PHONE_NUMBER + " TEXT, " + FAX + " TEXT, " + MO_TA_LOAI_HINH_KD + " TEXT, " + WEBSITE + " TEXT, " + URL_IMAGE + " TEXT, " + LAT + " TEXT, " + LONG + " TEXT, " + REG_UPD_DT + " INTEGER)";
    private static final String[] COLUMNS 			= { ID, MA_DIA_DIEM, TEN_DIA_DIEM, TEN_LOAI_HINH_KD, LOAI_DIA_DIEM, DIA_CHI, QUAN, HUYEN, PHUONG, THANH_PHO, PHONE_NUMBER, FAX, MO_TA_LOAI_HINH_KD, WEBSITE, URL_IMAGE, LAT, LONG, REG_UPD_DT };
	
	public DaoDiaDiem(Context context)
	{
		super(context);
	}
	
	private DSDiaDiem getParamSetDto(Cursor cursor)
	{
		int index = 1;
		DSDiaDiem dto = new DSDiaDiem(cursor.getString(index++), cursor.getString(index++),
				cursor.getString(index++), cursor.getString(index++), cursor.getString(index++),
				cursor.getString(index++), cursor.getString(index++), cursor.getString(index++),
				cursor.getString(index++), cursor.getString(index++), cursor.getDouble(index++),
				cursor.getDouble(index++), cursor.getString(index++), false,true);
        return dto;
    }
	
	// Lấy toàn bộ thông tin có trong table
	public List<DSDiaDiem> findAll()
	{
        List<DSDiaDiem> list = new ArrayList<DSDiaDiem>();
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null, TEN_LOAI_HINH_KD + " DESC," + REG_UPD_DT + " DESC");
        while (cursor.moveToNext())
        {
            list.add(getParamSetDto(cursor));
        }
        if (cursor != null)
        {
            cursor.close();
        }
        return list;
    }
	
	// Kiểm tra sự tồn tại của loại cửa hàng
	public DSDiaDiem findById(String mach)
	{
        String selection = MA_DIA_DIEM + " = '" + mach + "'";
        Cursor cursor = db.query(TABLE_NAME, COLUMNS, selection, null, null, null, null);
        try
        {
            if (cursor.moveToNext()) { return getParamSetDto(cursor); }
        }
        finally
        {
            if (cursor != null) { cursor.close(); }
        }
        return null;
    }
	
	// Xóa cửa hàng
	public int deleteById(String mach)
	{
		String selection = MA_DIA_DIEM + " = '" + mach + "'";
        return db.delete(TABLE_NAME, selection, null);
	}
	
	// Lưu thông tin cửa hàng
	public long insert(String mach, String tench, String tenloaikd, String loaich, String diachi, String quan, String huyen, String phuong, String thanhpho, String phone, 
			String fax, String mota, String website, String urlimage, String lat, String longs)
	{
		if(findById(mach) == null)
		{
			ContentValues values = new ContentValues();
			values.put("MA_DIA_DIEM", mach);
			values.put("TEN_DIA_DIEM", tench);
			values.put("TEN_LOAI_HINH_KD", tenloaikd);
			values.put("LOAI_DIA_DIEM", loaich);
			values.put("DIA_CHI", diachi);
			values.put("QUAN", quan);
			values.put("HUYEN", huyen);
			values.put("PHUONG", phuong);
			values.put("THANH_PHO", thanhpho);
			values.put("PHONE_NUMBER", phone);
			values.put("FAX", fax);
			values.put("MO_TA_LOAI_HINH_KD", mota);
			values.put("WEBSITE", website);
			values.put("URL_IMAGE", urlimage);
			values.put("LAT", lat);
			values.put("LONG", longs);
			values.put("REG_UPD_DT", CalendarUtil.getCurrentDate());
	        return db.insert(TABLE_NAME, null, values);
		}
		return -1;
    }
}